package com.zl.learning.lambada;

import com.zl.learning.lambada.model.User;
import org.junit.Test;

import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Test1 {
    @Test
    public void test1() {
        Stream<Double> randoms = Stream.generate(Math::random).limit(100);
        randoms.forEach(System.out::println);
        new Thread(() -> System.out.println("aaa"));
        int[] a = new int[]{0};
        Stream.generate(() -> a[0]++).limit(10).forEach(System.out::println);
    }

    @Test
    public void test2() {
        Object[] powers = Stream.iterate(1.0, p -> p * 2)
                .peek(e -> System.out.println("Fetching " + e))
                .limit(20).toArray();
    }

    @Test
    public void test3() {
        List<Integer> ins = Stream.iterate(1, n -> n * 2).limit(10).collect(Collectors.toList());
        ins.forEach(System.out::println);
        ins.sort(Comparator.comparing(Integer::intValue));
        System.out.println("--------------------");
        List<Integer> ins2 = new Random().ints().boxed().limit(10).collect(Collectors.toList());
        ins2.forEach(System.out::println);
        System.out.println("--------------------");
        List<Integer> ins3 = new Random().ints(1, 10).boxed().limit(10).collect(Collectors.toList());
        ins3.forEach(System.out::println);
        System.out.println("--------------------");
        HashSet<Integer> sets = new Random().ints(1, 10).boxed().limit(10).collect(HashSet::new, HashSet::add, HashSet::addAll);
        sets.forEach(System.out::println);
    }

    @Test
    public void test4() {
        Function<User, Integer> fun = User::getId;
        User user = new User();
        Supplier<Integer> consumer = user::getId;
        testFunc(User::getId, Arrays.asList(new User(1), new User(2)));
    }

    private <T, R> void testFunc(Function<T, R> func, List<T> users) {
        users.forEach(r -> System.out.println(func.apply(r)));
    }
}
